<?php 
	global $wpdb;
	
	///////////// get geta from database
	$id="";
	if(isset($_GET['id'])){
		if($_GET['action']=="edit"){
			$id = $_GET['id'];
			$edit = $wpdb -> get_row('select * from daysout Where id='.$id, ARRAY_A);
		}
	}
	
	/////// save data
	if(isset($_POST['nm_daysOut'])){
		$fv = array(	"place_of_interest"	=> $_POST['nm_weddingguru_place'],
						"nearest_town"		=> $_POST['nm_weddingguru_town'],
						"range_1"			=> $_POST['nm_weddingguru_0to5'],
						"range_2"			=> $_POST['nm_weddingguru_5to10'],
						"range_3"			=> $_POST['nm_weddingguru_10to15'],
						"range_4"			=> $_POST['nm_weddingguru_15to20'],
						"range_5"			=> $_POST['nm_weddingguru_20'],
						"link"				=> $_POST['nm_weddingguru_link']
			  );
		if($_POST['nm_weddingguru_id']=="")	  {		/////////// saving
			 $rs=$wpdb->insert("daysout", $fv);
		}	
		else { 				/////////// updating
			$w = array("id"	=>$_POST['nm_weddingguru_id']);
			$rs=$wpdb->update("daysout", $fv, $w);
			$rs = true;
		}

		if($rs){
			$msg="Record Successfully Added/Updated";
			$class = "updated fade";
		} else {
			$msg = "Record Not Added/Updated Successfully";
			$class = "error";
		}
		 
		 //$wpdb->show_errors();
		 //$wpdb->print_error();
		unset($_POST['nm_daysOut']);
	}
	
	
	
	///////////////////////// Pagination //////////////////////////////
	$totalRowsPerPage=50;
	function makePaginationUrl($pageNo){
		$pVar='pgNo';
		$q=explode("&",$_SERVER['QUERY_STRING']);
		$done=false;
		for($i=0;$i<count($q);$i++){
			if(substr($q[$i],0,strlen($pVar)+1)==$pVar."="){
				$q[$i]=$pVar."=".$pageNo;
				$done=true;
				break;
			}
		}
		if($done!=true) $q[]=$pVar."=".$pageNo;
		$q=implode("&",$q);
		return "?".$q;
	}
	
	function makePaginationNext($totalRec, $totalRowsPerPage){
		if(isset($_GET['pgNo'])) $curPage=$_GET['pgNo']; else $curPage=1;
		$tp= ceil($totalRec/$totalRowsPerPage);
		if($curPage!=$tp) return makePaginationUrl($curPage+1); else return "";
	}

	function makePaginationPrevious(){
		if(isset($_GET['pgNo'])) $curPage=$_GET['pgNo']; else $curPage=1;
		if($curPage!=1) return makePaginationUrl($curPage-1); else return "";
	}
	
	function makePaginationCombo($totalRec, $totalRowsPerPage){
		$curPage=$_GET['pgNo'];
		$tp= ceil($totalRec/$totalRowsPerPage);
		$combo="<select onchange='javascript: window.location=this.value;'>";
		for($i=1;$i<=$tp;$i++){
			if($curPage==$i) $sel="selected='selected'"; else $sel="";
			$combo.="<option value='".makePaginationUrl($i)."' $sel>Page $i</option>\n";
		}
		$combo.="</select>";
		return $combo;
	}
	
	function makeLimit($totalRowsPerPage){
		if(isset($_GET['pgNo'])) $pageNo=$_GET['pgNo']; else $pageNo=1;
		$offset=($pageNo-1)*$totalRowsPerPage;
		return " limit ".$offset.",".$totalRowsPerPage;
	}

	function fixRequestURI($vars){
		$uri = str_replace( '%7E', '~', $_SERVER['REQUEST_URI']);
		$parts = explode("?", $uri);
		
		$qsArr = array();
		if(isset($parts[1])){	////// query string present explode it
			$qsStr = explode("&", $parts[1]);
			foreach($qsStr as $qv){
				$p = explode("=",$qv);
				$qsArr[$p[0]] = $p[1];
			}
		}
		
		
		
		//////// updatig query string
		foreach($vars as $key=>$val){
			if($val==NULL) unset($qsArr[$key]); else $qsArr[$key]=$val;
		}
		
		////// rejoin query string
		$qsStr="";
		foreach($qsArr as $key=>$val){
			$qsStr.=$key."=".$val."&";
		}
		if($qsStr!="") $qsStr=substr($qsStr,0,strlen($qsStr)-1);
		$uri = $parts[0];
		if($qsStr!="") $uri.="?".$qsStr;
		return $uri;
	} 

	
	if(isset($_GET['county'])) $criteria="nearest_town = '".$_GET['county']."'"; else $criteria="1=1";
	$data = $wpdb -> get_results('select * from daysout Where '.$criteria.makeLimit($totalRowsPerPage), ARRAY_A);
	$tRec = $wpdb -> get_results('select COUNT(*) as c from daysout Where '.$criteria,  ARRAY_A);
	$tRec = intval($tRec[0]["c"]);
	
	$counties = $wpdb -> get_results('select DISTINCT(nearest_town)  from daysout where nearest_town<>"" order by nearest_town', ARRAY_A);
?>

<script type="text/javascript">
function isValidURL(url){
}
function nm_validate_daysout(){
	if(jQuery("#nm_weddingguru_place").val()==""){
		alert("Please Provide Place Of Interest");
		jQuery("#nm_weddingguru_place").focus();
		return false;
	}

	if(jQuery("#nm_weddingguru_town").val()==""){
		alert("Please Provide Nearest Town");
		jQuery("#nm_weddingguru_town").focus();
		return false;
	}

	return true;
}
</script>

<h2>Days Out</h2>

<?php if(isset($msg)): ?>
<div id="message" class="<?php echo $class ?>"><?php echo $msg ?></div>
<?php endif; ?>

<form action="<?php echo fixRequestURI(array("action"=>$action)) ?>" method="post" id="frmDaysOut" onsubmit="return nm_validate_daysout()">
<input type="hidden" name="nm_weddingguru_id" value="<?php echo $id ?>" />
<table>
<tr>
	<td>Place Of Interest</td>
    <td><input type="text" name="nm_weddingguru_place" id="nm_weddingguru_place" value="<?php echo $edit['place_of_interest']?>" class="regular-text" ></td>
</tr>

<tr>
	<td>Nearest Town</td>
    <td><input type="text" name="nm_weddingguru_town" id="nm_weddingguru_town" value="<?php echo $edit['nearest_town']?>" class="regular-text" ></td>
</tr>

<tr>
	<td>£0 - 5</td>
    <td>
    <select name="nm_weddingguru_0to5" style="width:200px;">
    	<option value="Y" <?php if(strtoupper($edit['range_1'])=="Y" or strtoupper($edit['range_1'])=="YES") echo 'selected="selected"'?> >Yes </option>
        <option value="" <?php if($edit['range_1']=="") echo 'selected="selected"'?>>No </option>
    </select>
    </td>
</tr>

<tr>
	<td>£5 - 10</td>
    <td>
    <select name="nm_weddingguru_5to10" style="width:200px;">
    	<option value="Y" <?php if(strtoupper($edit['range_2'])=="Y" or strtoupper($edit['range_2'])=="YES") echo 'selected="selected"'?> >Yes </option>
        <option value="" <?php if($edit['range_2']=="") echo 'selected="selected"'?>>No </option>
    </select>
    </td>
</tr>

<tr>
	<td>£10 - 15</td>
    <td>
    <select name="nm_weddingguru_10to15"  style="width:200px;">
    	<option value="Y" <?php if(strtoupper($edit['range_3'])=="Y" or strtoupper($edit['range_3'])=="YES") echo 'selected="selected"'?> >Yes </option>
        <option value="" <?php if($edit['range_3']=="") echo 'selected="selected"'?>>No </option>
    </select>
    </td>
</tr>

<tr>
	<td>£15 - 20</td>
    <td>
    <select name="nm_weddingguru_15to20"  style="width:200px;">
    	<option value="Y" <?php if(strtoupper($edit['range_4'])=="Y" or strtoupper($edit['range_4'])=="YES") echo 'selected="selected"'?> >Yes </option>
        <option value="" <?php if($edit['range_4']=="") echo 'selected="selected"'?>>No </option>
    </select>
    </td>
</tr>

<tr>
	<td>£20</td>
    <td>
    <select name="nm_weddingguru_20"  style="width:200px;">
    	<option value="Y" <?php if(strtoupper($edit['range_5'])=="Y" or strtoupper($edit['range_5'])=="YES") echo 'selected="selected"'?> >Yes </option>
        <option value="" <?php if($edit['range_5']=="") echo 'selected="selected"'?>>No </option>
    </select>
    </td>
</tr>

<tr>
	<td>Website Link</td>
    <td><input type="text" name="nm_weddingguru_link" id="nm_weddingguru_link" value="<?php echo $edit['link']?>" class="regular-text" ></td>
</tr>


<tr>
    <td colspan="2"><input type="submit" value="Save" class="button" name="nm_daysOut"></td>
</tr>

</table>
</form>


<br />
<br />
<br />

<table cellspacing="0" class="wp-list-table widefat fixed posts" id="lstDaysOut">
    <tr>
        <th width="283" align="center">Place Of Interest</th>
        <th width="168" align="center">Nearest Town</th>
        <th width="45" align="center">£0 - 5</th>
        <th width="45" align="center">£5 - 10</th>
        <th width="45" align="center">£10 - 15</th>
        <th width="45" align="center">£15 - 20 </th>
        <th width="45" align="center">£20 </th>
        <th width="84" align="center">Website</th>
    </tr>
	<?php foreach($data as $dataItem):
		$edit = fixRequestURI( array("action"=>"edit", "id"	=> $dataItem['id']) );
	?>
    <tr>
        <td align="left"><?=$dataItem['place_of_interest']?><br />
				<a href="<?php echo $edit ?>">Edit</a>
        </td>
        <td align="left"><?=$dataItem['nearest_town']?></td>
        <td width="45" align="center"><?=$dataItem['range_1']?></td>
        <td width="45" align="center"><?=$dataItem['range_2']?></td>
        <td width="45" align="center"><?=$dataItem['range_3']?></td>
        <td width="45" align="center"><?=$dataItem['range_4']?></td>
        <td width="45" align="center"><?=$dataItem['range_5']?></td>
        <td align="center">
        <?php if($dataItem['link']!="#VALUE!" or $dataItem['link']!=""): ?>
            <a href="<?=$dataItem['link']?>">Visit site</a>
         <?php endif; ?>
        </td>
    </tr>
    <?php endforeach; ?>
    
</table>


<br />
<br />

<div id="paginationBar" style="width:100%; margin:0 auto; text-align:right; margin-top:5px;">
<?php
$prevPage=makePaginationPrevious();
$nextPage=makePaginationNext($tRec, $totalRowsPerPage);
?>
<?php if($prevPage!=""): ?>
<a href="<?=$prevPage?>">&lt;&lt;Prev</a>&nbsp;
<?php endif; ?>
<?=makePaginationCombo($tRec, $totalRowsPerPage);?>&nbsp;
<?php if($nextPage!=""): ?>
<a href="<?=$nextPage?>">Next&gt;&gt;</a>&nbsp;
<?php endif; ?>
</div>    
            
